home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / bb_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  5.8 KB  |  251 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  bb_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17. #ifndef BBTREEH
  18. #define BBTREEH
  19.  
  20. //--------------------------------------------------------------------
  21. //  
  22. //  BB[alpha] Trees
  23. //
  24. //  Michael Wenzel   (1989)
  25. //
  26. // Implementation follows
  27. // Kurt Mehlhorn: Data Structures and Algorithms 1, section III.5.1
  28. //
  29. // Aenderungen:
  30. //   - virtuelle compare-Funktion   (M. Wenzel, Nov. 1989)
  31. //--------------------------------------------------------------------
  32.  
  33.  
  34. #include <LEDA/b_stack.h>
  35.  
  36.  
  37. // -----------------------------------------------------
  38. // declarations and definitions
  39. // -------------------------------------------------------
  40.  
  41. const int BSTACKSIZE = 32 ; // according to tree.size and alpha
  42.  
  43. const float SQRT1_2 = 0.70710678;
  44.  
  45. class bb_node;
  46. class bb_tree;
  47.  
  48. typedef bb_node* bb_item;
  49.  
  50. typedef bb_node* bb_tree_item;
  51.  
  52. declare(b_stack,bb_item);
  53. typedef b_stack(bb_item) bb_tree_stack;
  54.  
  55.  
  56. typedef void (*DRAW_BB_NODE_FCT)(double,double,void*);
  57. typedef void (*DRAW_BB_EDGE_FCT)(double,double,double,double);
  58.  
  59. class bb_node {
  60.  
  61.   ent ke;
  62.   ent inf;
  63.   int gr;
  64.   bb_item sohn[2];
  65.  
  66.   friend class bb_tree;
  67.   friend class range_tree;
  68.   friend class Segment_Tree;
  69.   friend class seg_node_tree;
  70.  
  71. public:
  72.  
  73.   ent key()           { return ke; }
  74.   ent info()          { return inf; }
  75.  
  76.   int blatt()         { return (gr==1); }
  77.   int groesse()       { return gr; }
  78.  
  79.   float bal()
  80.     { if (blatt()) return 0.5 ;
  81.       else return float(float(sohn[0]->groesse())/float(gr));
  82.         }
  83.  
  84.   bb_node(ent k=0,ent i=0,int leaf=0,bb_item ls=0,bb_item rs=0)
  85.     { ke = k;
  86.       inf = i;
  87.       sohn[0] = ls;
  88.       sohn[1] = rs;
  89.       if (leaf==0)
  90.     gr=1;
  91.       else gr = ls->groesse()+rs->groesse();
  92.     }
  93.  
  94.   bb_node(bb_item p)
  95.     { 
  96.       ke = p->key();
  97.       inf = p->info();
  98.       gr = p->groesse();
  99.       sohn[0] = p->sohn[0];
  100.       sohn[1] = p->sohn[1];
  101.     }
  102.       
  103.   OPERATOR_NEW(5)
  104.   OPERATOR_DEL(5)
  105.  
  106. }; 
  107.  
  108.  
  109.  
  110. class bb_tree {
  111.  
  112.   bb_item root;
  113.   bb_item first;
  114.   bb_item iterator;
  115.   int   anzahl; 
  116.   float alpha;
  117.   float d;
  118.   bb_tree_stack st;
  119.  
  120.   friend class bb_node;
  121.   friend class range_tree;
  122.   friend class seg_node_tree;
  123.   friend class Segment_Tree;
  124.  
  125.   int   blatt(bb_item it)
  126.     { return (it) ? it->blatt() : 0; }
  127.  
  128.   void  lrot(bb_item , bb_item ); 
  129.   void  rrot(bb_item , bb_item ); 
  130.   void  ldrot(bb_item , bb_item ); 
  131.   void  rdrot(bb_item , bb_item ); 
  132.  
  133.   void  deltree(bb_item);
  134.   bb_item copytree(bb_item , bb_item , bb_item& );
  135.   bb_item search(ent);
  136.   bb_item sinsert(ent , ent );
  137.   bb_item sdel(ent );
  138.  
  139. public:
  140.  
  141.   virtual int cmp(ent& x,ent& y) { return int(x)-int(y); }
  142.   virtual void clear_key(ent& x) const { Clear(x); }
  143.   virtual void clear_inf(ent& x) const { Clear(x); }
  144.  
  145.   virtual void copy_key(ent& x)  const { Copy(x);  }
  146.   virtual void copy_inf(ent& x)  const { Copy(x);  }
  147.  
  148.   ent     key(bb_item it)  const   { return it->ke;  }
  149.   ent&    info(bb_item it)         { return it->inf; }
  150.   ent     inf(bb_item it)  const   { return it->inf; }
  151.   ent     translate(ent y);
  152.  
  153.   bb_item insert(ent ,ent );
  154.   bb_item change_obj(ent ,ent );
  155.   bb_item change_inf(bb_item it,ent y)
  156.           { if (it)  
  157.         { it->inf = y;
  158.           return it; }
  159.         else return 0;
  160.               }
  161.   
  162.  
  163.   bb_item del(ent );
  164.  
  165.  
  166.   void del_item(bb_item it) { if (it) del(it->key()); }
  167.   void del_min() { if (first) del(first->key()); } 
  168.   void decrease_key(bb_item p, ent k) { ent i = p->info(); 
  169.                                         //copy_inf(i);
  170.                                         del(p->key());
  171.                                         insert(k,i);
  172.                                         //clear_inf(i);
  173.                                        }
  174.  
  175.   bb_item locate(ent );
  176.   bb_item located(ent );
  177.   bb_item lookup(ent );
  178.  
  179.   bb_item cyclic_succ(bb_item it)  const
  180.         { return it ? it->sohn[1] : 0 ; }
  181.   bb_item cyclic_pred(bb_item it)  const
  182.       { return it ? it->sohn[0] : 0 ; }
  183.   bb_item succ(bb_item it) const
  184.           { return ( it && it->sohn[1] != first ) ? it->sohn[1] : 0 ; }
  185.   bb_item pred(bb_item it) const
  186.           { return ( it && it != first ) ? it->sohn[0] : 0 ; }
  187.  
  188.   bb_item ord(int k);
  189.   bb_item min()       const    { return (first) ? first : 0; }
  190.   bb_item find_min()  const    { return (first) ? first : 0; }
  191.   bb_item max()       const    { return (first) ? first->sohn[0] : 0 ; }
  192.  
  193.   bb_item first_item() const         { return (first) ? first : 0; }
  194.   bb_item next_item(bb_item x) const { return succ(x); }
  195.  
  196.   bb_item move_iterator() ;
  197.  
  198.   int current_item(bb_item& x) 
  199.      { if (!iterator) return 0;
  200.        else { x = iterator; return 1; }
  201.      }
  202.  
  203.  
  204.   void init_iterator()          { iterator = 0; }
  205.   void  lbreak()                { iterator = 0; }
  206.  
  207.  
  208.   void  clear();
  209.   int   size()    const        { return anzahl; }
  210.   int   empty()   const        { return (anzahl==0) ? true : false; }
  211.  
  212.   int   member(ent );
  213.   bb_tree& operator=(bb_tree& w);
  214.  
  215.   void  set_alpha(float a) 
  216.         { if (anzahl>=3)
  217.              error_handler(4,"aenderung von alpha nicht erlaubt");
  218.           if ((a<0.25) || (a>1-SQRT1_2))
  219.              error_handler(3,"alpha not in range");
  220.           alpha=a;
  221.           d = 1/(2-alpha) ;
  222.         }
  223.  
  224.   float get_alpha() { return alpha; }
  225.  
  226.   bb_tree() : st(BSTACKSIZE)
  227.   { 
  228.     root = first = iterator = 0;
  229.     anzahl = 0;
  230.     alpha=0.28;
  231.     d=1/(2-alpha);
  232.   }
  233.  
  234.   bb_tree(float);
  235.   bb_tree(bb_tree&);
  236.  
  237.   ~bb_tree()  { clear(); }
  238.  
  239.  
  240.   void draw(DRAW_BB_NODE_FCT, DRAW_BB_EDGE_FCT, bb_node*,
  241.           double, double, double, double, double);
  242.  
  243.   void draw(DRAW_BB_NODE_FCT, DRAW_BB_EDGE_FCT, 
  244.           double, double, double, double);
  245.  
  246. };
  247.  
  248.  
  249. #endif
  250.